//
//  NSString+Caculate.m
//  CoinXman
//
//  Created by liuxuan on 2018/5/14.
//  Copyright © 2018年 liuxuan. All rights reserved.
//

#define IsValidString(string) (string && [string isKindOfClass:[NSString class]] && [string length])
#define IsEmptyString(string) (string && [string isKindOfClass:[NSString class]] && [string length] == 0)

#import "NSString+Caculate.h"

@implementation NSString (Caculate)

- (NSString *)stringByAdding:(NSString *)bString Decimals:(NSInteger)Decimals{
    NSString *aString = self;
    if (IsEmptyString(self) ) {
        aString = @"0";
    }
    if (IsEmptyString(bString)) {
        bString = @"0";
    }
    NSDecimalNumber *num_1 = [NSDecimalNumber decimalNumberWithString:aString];
    NSDecimalNumber *num_2 = [NSDecimalNumber decimalNumberWithString:bString];
    NSDecimalNumberHandler *handel = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:Decimals raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    
    NSDecimalNumber *num_3 = [num_1 decimalNumberByAdding:num_2 withBehavior:handel];
    //NSLog(@"num_3 == %@",num_3.stringValue);
    NSString *rid = [num_3.stringValue ridTail];
    return rid;
}
- (NSString *)stringBySubtracting:(NSString *)bString Decimals:(NSInteger)Decimals{
    NSString *aString = self;
    if ( IsEmptyString(self) ) {
        aString = @"0";
    }
    if (IsEmptyString(bString)) {
        bString = @"0";
    }
    NSDecimalNumber *num_1 = [NSDecimalNumber decimalNumberWithString:aString];
    NSDecimalNumber *num_2 = [NSDecimalNumber decimalNumberWithString:bString];
    NSDecimalNumberHandler *handel = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:Decimals raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    
    NSDecimalNumber *num_3 = [num_1 decimalNumberBySubtracting:num_2 withBehavior:handel];
    //NSLog(@"num_3 == %@",num_3.stringValue);
    NSString *rid = [num_3.stringValue ridTail];
    return rid;
}
- (NSString *)stringByMultiplyingBy:(NSString *)bString Decimals:(NSInteger)Decimals{
    NSString *aString = self;
    if (IsEmptyString(self) ) {
        aString = @"0";
    }
    if (IsEmptyString(bString)) {
        bString = @"0";
    }
    NSDecimalNumber *num_1 = [NSDecimalNumber decimalNumberWithString:aString];
    NSDecimalNumber *num_2 = [NSDecimalNumber decimalNumberWithString:bString];
    
    
    NSDecimalNumberHandler *handel = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:Decimals raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    
    NSDecimalNumber *num_3 = [num_1 decimalNumberByMultiplyingBy:num_2 withBehavior:handel];
    //NSLog(@"num_3 == %@",num_3.stringValue);
    NSString *rid = [num_3.stringValue ridTail];
    return rid;
}


- (NSString *)stringByDividingBy:(NSString *)bString Decimals:(NSInteger)Decimals{
    NSString *aString = self;
    if ( IsEmptyString(self) ) {
        aString = @"0";
    }
    if (IsEmptyString(bString)) {
        bString = @"0";
    }
    if ([[bString ridTail] isEqualToString:@"0"]) {
        NSLog(@"除数为0");
        return @"0";
    }
    NSDecimalNumber *num_1 = [NSDecimalNumber decimalNumberWithString:aString];
    NSDecimalNumber *num_2 = [NSDecimalNumber decimalNumberWithString:bString];
    NSDecimalNumberHandler *handel = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:Decimals raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    
    
    NSDecimalNumber *num_3 = [num_1 decimalNumberByDividingBy:num_2 withBehavior:handel];
    //NSLog(@"num_3 == %@",num_3.stringValue);
    NSString *rid = [num_3.stringValue ridTail];
    return rid;
}


- (NSString *)stringByMultiplyingByGwei
{
    NSString *aString = self;
    if ( IsEmptyString(self) ) {
        aString = @"0";
    }
    NSDecimalNumber *a = [[NSDecimalNumber alloc] initWithString:aString];
    NSDecimalNumber *c = [a decimalNumberByMultiplyingByPowerOf10:9];
    NSString *cString = [NSString stringWithFormat:@"%@", c];
    return  cString;
}

- (NSString *)stringByMultiplyingBywei
{
    NSString *aString = self;
    if ( IsEmptyString(self) ) {
        aString = @"0";
    }
    NSDecimalNumber *a = [[NSDecimalNumber alloc] initWithString:aString];
    NSDecimalNumber *c = [a decimalNumberByMultiplyingByPowerOf10:18];
    NSString *cString = [NSString stringWithFormat:@"%@", c];
    return  cString;
}

- (NSString *)stringByMultiplyingByDecimals:(NSInteger)Decimals
{
    NSString *aString = self;
    if ( IsEmptyString(self) ) {
        aString = @"0";
    }
    NSDecimalNumber *a = [[NSDecimalNumber alloc] initWithString:aString];
    NSDecimalNumber *c = [a decimalNumberByMultiplyingByPowerOf10:Decimals];
    NSString *cString = [NSString stringWithFormat:@"%@", c];
    return  cString;
}

- (NSString *)stringByMultiplyingBySatoshi
{
    NSString *aString = self;
    if ( IsEmptyString(self) ) {
        aString = @"0";
    }
    NSDecimalNumber *a = [[NSDecimalNumber alloc] initWithString:aString];
    NSDecimalNumber *c = [a decimalNumberByMultiplyingByPowerOf10:8];
    NSString *cString = [NSString stringWithFormat:@"%@", c];
    return  cString;
}


- (BOOL)isBig:(NSString *)bString {
    NSString *aString = self;
    if ( IsEmptyString(self) ) {
        aString = @"0";
    }
    if (IsEmptyString(bString)) {
        bString = @"0";
    }
    NSDecimalNumber *a = [[NSDecimalNumber alloc] initWithString:aString];
    NSDecimalNumber *b = [[NSDecimalNumber alloc] initWithString:bString];
    if ([a compare:b] == NSOrderedAscending) {//上升
        return NO;
    } else if ([a compare:b] == NSOrderedDescending) {//下降
        return YES;
    } else {//相等
        return NO;
    }
}

- (BOOL)isEqualValue:(NSString *)bString {
    NSString *aString = self;
    if ( IsEmptyString(self) ) {
        aString = @"0";
    }
    if (IsEmptyString(bString)) {
        bString = @"0";
    }

    NSDecimalNumber *a = [[NSDecimalNumber alloc] initWithString:aString];
    NSDecimalNumber *b = [[NSDecimalNumber alloc] initWithString:bString];
    if ([a compare:b] == NSOrderedAscending) {//上升
        return NO;
    } else if ([a compare:b] == NSOrderedDescending) {//下降
        return NO;
    } else {//相等
        return YES;
    }
}

- (NSComparisonResult)ob_compare:(NSString *)bString {
    NSString *aString = self;
    if ( IsEmptyString(self) ) {
        aString = @"0";
    }
    if (IsEmptyString(bString)) {
        bString = @"0";
    }
    NSDecimalNumber *a = [[NSDecimalNumber alloc] initWithString:aString];
    NSDecimalNumber *b = [[NSDecimalNumber alloc] initWithString:bString];
    return [a compare:b];
}

- (NSString *)ridTail {
    NSString *string = self;
    if (![string containsString:@"."]) {
        return string;
    }
    if ([string hasSuffix:@"0"]) {
        string = [string substringToIndex:string.length - 1];
        string = [string ridTail];
    } else if ([string hasSuffix:@"."]) {
        string = [string substringToIndex:string.length - 1];
        return string;
    } else {
        return string;
    }
    return string;
}

+ (NSString *)formatterNumber:(NSNumber *)number {
    return [self formatterNumber:number fractionDigits:2];
}

+ (NSString *)formatterNumber:(NSNumber *)number fractionDigits:(NSUInteger)fractionDigits {
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setMaximumFractionDigits:fractionDigits];
    [numberFormatter setMinimumFractionDigits:0];
    [numberFormatter setMinimumIntegerDigits:1];
    
    return [numberFormatter stringFromNumber:number];
}


- (NSNumber *)numberFromString
{
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
    NSNumber *number = [numberFormatter numberFromString:self];
    return number;
}


- (NSString *)DecimalString:(NSInteger)decimal
{
    NSDecimalNumber *a = [[NSDecimalNumber alloc] initWithString:self];

    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.numberStyle = NSNumberFormatterDecimalStyle;
    formatter.maximumFractionDigits = decimal;//最多保留几位小数，就是几
    formatter.groupingSeparator = @"";
    return [formatter stringFromNumber:a];
}
@end
